// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

const stringutils = require('./stringutils');

let timer = null;
let pasteStr = "";

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	timer = setInterval(() => {
		vscode.env.clipboard.readText().then(str => {
			pasteStr = str;
		});
	}, 200);

	//转换
	let toUpperCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.toUpperCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		edit.replace(new vscode.Range(start, end), stringutils.ToUpperCase(str));
	})
	let toLowerCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.toLowerCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		edit.replace(new vscode.Range(start, end), stringutils.ToLowerCase(str));
	})
	let toBigCamelCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.toBigCamelCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		edit.replace(new vscode.Range(start, end), stringutils.ToBigCamelCase(str));
	})
	let toSmallCamelCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.toSmallCamelCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		edit.replace(new vscode.Range(start, end), stringutils.ToSmallCamelCase(str));
	})

	context.subscriptions.push(toUpperCase);
	context.subscriptions.push(toLowerCase);
	context.subscriptions.push(toBigCamelCase);
	context.subscriptions.push(toSmallCamelCase);

	//粘贴
	let pasteToUpperCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.pasteToUpperCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		if (start == end) {
			edit.insert(end, stringutils.ToUpperCase(pasteStr))
		} else {
			edit.replace(new vscode.Range(start, end), stringutils.ToUpperCase(pasteStr));
		}
	})
	let pasteToLowerCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.pasteToLowerCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		if (start == end) {
			edit.insert(end, stringutils.ToLowerCase(pasteStr))
		} else {
			edit.replace(new vscode.Range(start, end), stringutils.ToLowerCase(pasteStr));
		}
	})
	let pasteToBigCamelCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.pasteToBigCamelCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		if (start == end) {
			edit.insert(end, stringutils.ToBigCamelCase(pasteStr))
		} else {
			edit.replace(new vscode.Range(start, end), stringutils.ToBigCamelCase(pasteStr));
		}
	})
	let pasteToSmallCamelCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.pasteToSmallCamelCase', (textEditor, edit) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		if (start == end) {
			edit.insert(end, stringutils.ToSmallCamelCase(pasteStr))
		} else {
			edit.replace(new vscode.Range(start, end), stringutils.ToSmallCamelCase(pasteStr));
		}
	})

	context.subscriptions.push(pasteToUpperCase);
	context.subscriptions.push(pasteToLowerCase);
	context.subscriptions.push(pasteToBigCamelCase);
	context.subscriptions.push(pasteToSmallCamelCase);

	//复制
	let copyToUpperCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.copyToUpperCase', (textEditor) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		vscode.env.clipboard.writeText(stringutils.ToUpperCase(str));
		vscode.window.showInformationMessage('转换后的内容已复制至剪切板');
	})
	let copyToLowerCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.copyToLowerCase', (textEditor) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		vscode.env.clipboard.writeText(stringutils.ToLowerCase(str));
		vscode.window.showInformationMessage('转换后的内容已复制至剪切板');
	})
	let copyToBigCamelCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.copyToBigCamelCase', (textEditor) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		vscode.env.clipboard.writeText(stringutils.ToBigCamelCase(str));
		vscode.window.showInformationMessage('转换后的内容已复制至剪切板');
	})
	let copyToSmallCamelCase = vscode.commands.registerTextEditorCommand('kjxing-plugin.copyToSmallCamelCase', (textEditor) => {
		let start = textEditor.selection.start;
		let end = textEditor.selection.end;
		let str = textEditor.document.getText(new vscode.Range(start, end));
		vscode.env.clipboard.writeText(stringutils.ToSmallCamelCase(str));
		vscode.window.showInformationMessage('转换后的内容已复制至剪切板');
	})

	context.subscriptions.push(copyToUpperCase);
	context.subscriptions.push(copyToLowerCase);
	context.subscriptions.push(copyToBigCamelCase);
	context.subscriptions.push(copyToSmallCamelCase);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {
	clearInterval(timer);
}

module.exports = {
	activate,
	deactivate
}